package org.asdmp.restday.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.asdmp.restday.service.RestDayService;
import org.jbpm.api.task.Task;

/**
 * Servlet implementation class CompleteTaskController
 */
@WebServlet("/controller/completeTaskController")
public class CompleteTaskController extends HttpServlet implements Servlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CompleteTaskController() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");
		RestDayService.startUp();
		String assignee = request.getParameter("to");
		List<Task> tasks = RestDayService.taskService.findPersonalTasks(assignee);
		RestDayService.print("tasksize", tasks.size());
		if (tasks!= null && tasks.size()>0){
			RestDayService.taskService.completeTask(tasks.get(0).getId());	
			RestDayService.print("Jerry", "complete");
			request.setAttribute("fromnode","["+assignee+"]");
			request.setAttribute("node","[结束]");
			request.setAttribute("responseText","Jerry========Complete");
			request.getRequestDispatcher("../jsp/console.jsp").forward(request, response);
			return;
		}
		RestDayService.print("Jerry", "complete");
		request.setAttribute("fromnode","[结束]");
		request.setAttribute("node","[结束]");
		request.setAttribute("responseText","Jerry========Complete");
		request.getRequestDispatcher("../jsp/console.jsp").forward(request, response); 
	}

}
